package com.cheli.manager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheli.commonutils.R;
import com.cheli.manager.entity.ManaSection;
import com.cheli.manager.entity.vo.SectionQuery;
import com.cheli.manager.service.ManaSectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author cheli
 * @since 2021-03-21
 */
@RestController
@RequestMapping("/manager/section")
@CrossOrigin
@Api(value = "section", tags = "部门管理")
@Slf4j
public class ManaSectionController {
    
    private final ManaSectionService manaSectionService;
    
    @Autowired
    public ManaSectionController(ManaSectionService manaSectionService){
        this.manaSectionService = manaSectionService;
    }

   @ApiOperation("所有部门列表")
    @GetMapping("findAll")
    public R findAllSection(){
        log.info("查询所有部门");
        List<ManaSection> list = manaSectionService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation("逻辑删除部门")
    @DeleteMapping("{id}")
    public R removeById(@PathVariable String id) {
        log.info("删除部门"+ id);

        boolean flag = manaSectionService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("部门分页查询")
    @GetMapping("pageSection/{current}/{limit}")
    public R pageListSection(@PathVariable long current,
                           @PathVariable long limit) {
        log.info("部门分页查询");

       Page<ManaSection> page = new Page<>(current, limit);
        manaSectionService.page(page, null);

        long total = page.getTotal();
        List<ManaSection> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("部门条件分页查询")
    @PostMapping("pageSectionCondition/{current}/{limit}")
   public R pageSectionCondition(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) SectionQuery sectionQuery) {
        log.info("部门条件查询" + sectionQuery.toString());

       Page<ManaSection> page = new Page<>(current, limit);
        QueryWrapper<ManaSection> wrapper = new QueryWrapper<>();


        String begin = sectionQuery.getBegin();
        String end = sectionQuery.getEnd();
        String name = sectionQuery.getName();


        if (!StringUtils.isEmpty(name)) {
            wrapper.like("sectionName", name);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        wrapper.orderByAsc("sort");

        manaSectionService.page(page, wrapper);
        long total = page.getTotal();
        List<ManaSection> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }


   @ApiOperation("添加部门")
    @PostMapping("addSection")
    public R addSection(@RequestBody ManaSection manaSection) {
        log.info("添加部门" + manaSection);
        boolean save = manaSectionService.save(manaSection);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

   @GetMapping("getSection/{id}")
    @ApiOperation("根据ID查询部门")
    public R getSectionById(@PathVariable String id) {
        ManaSection manaSection = manaSectionService.getById(id);
        return R.ok().data("section", manaSection);
    }

    @PostMapping("updateSection")
    @ApiOperation("更改部门")
    public R updateSection(@RequestBody ManaSection section) {
        boolean update = manaSectionService.updateById(section);
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    
}

